<template>
  <common-layout>
    <div>
      <div class="top">
        <div class="header">
          <img alt="logo" class="logo" src="/image/u76.png" />
          <span class="title">地府管理系统</span>
        </div>
        <div class="desc"></div>
      </div>
      <div class="login">
        <a-form @submit="onSubmit" :form="form">
          <a-form-item>
            <a-input
              autocomplete="autocomplete"
              size="large"
              placeholder="报上名来"
              v-decorator="[
                'name',
                {
                  rules: [
                    {
                      required: true,
                      message: '嘚,报上名来',
                      whitespace: true,
                    },
                  ],
                },
              ]"
            >
              <a-icon slot="prefix" type="user" />
            </a-input>
          </a-form-item>
          <a-form-item>
            <a-input
              size="large"
              placeholder="呈上文牒"
              autocomplete="autocomplete"
              type="password"
              v-decorator="[
                'password',
                {
                  rules: [
                    {
                      required: true,
                      message: '呈上通关文牒',
                      whitespace: true,
                    },
                  ],
                },
              ]"
            >
              <a-icon slot="prefix" type="lock" />
            </a-input>
          </a-form-item>

          <div>
            <a-checkbox :checked="true">自动登录</a-checkbox>
            <a style="float: right">忘记密码</a>
          </div>
          <a-form-item>
            <a-button
              :loading="logging"
              style="width: 100%; margin-top: 24px"
              size="large"
              htmlType="submit"
              type="primary"
              >登录</a-button
            >
          </a-form-item>
        </a-form>
      </div>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from "@/layouts/CommonLayout";
import { login, getLoginUser, SetToken } from "@/api/user";
// import { setAuthorization } from "@/utils/request";
import { loadRoutes } from "@/utils/routerUtil";
import { mapMutations } from "vuex";

export default {
  name: "Login",
  components: { CommonLayout },
  data() {
    return {
      logging: false,
      error: "",
      form: this.$form.createForm(this),
    };
  },
  computed: {
    systemName() {
      return this.$store.state.setting.systemName;
    },
  },
  methods: {
    ...mapMutations("account", ["setUser", "setPermissions", "setRoles"]),
    onSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true;
          const name = this.form.getFieldValue("name");
          const password = this.form.getFieldValue("password");
          login(name, password).then(this.afterLogin);
        }
      });
    },
    afterLogin(res) {
      this.logging = false;
      if (res.code == 200) {
        SetToken(res.data);
        // setAuthorization({
        //   token: res.data,
        //   expireAt: new Date(6000),
        // });
        // const { user, permissions, roles } = loginRes.data;
        getLoginUser().then((res) => {
          if (res.code == 200) {
            this.setUser(res.data);
            this.$router.push("/home");
            loadRoutes([]);
          } else {
            this.$message.error(res.message, 3);
          }
        });
        // this.setUser(user);
        // this.setPermissions(permissions);
        // this.setRoles(roles);

        // this.$router.push("/home");
        // // 获取路由配置
        // getRoutesConfig().then((result) => {
        //   const routesConfig = result.data.data;
        // //   loadRoutes(routesConfig);

        //   this.$message.success(loginRes.message, 3);
        // });
      } else {
        this.$message.error(res.message, 3);
        this.error = res.message;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.common-layout {
  height: 100vh;
  overflow: auto;
  background-color: @layout-body-background;
  background-repeat: no-repeat;
  background-position-x: center;
  background-position-y: 0px;
  background-size: 100%;
  background-image: url("/image/bg.jpeg") !important;
  .content {
    padding: 32px 0;
    flex: 1;
    @media (min-width: 768px) {
      padding: 15% 0 24px !important;
    }
  }
  .top {
    text-align: center;
    .header {
      height: 44px;
      line-height: 44px;
      a {
        text-decoration: none;
      }
      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
      .title {
        font-size: 33px;
        color: "rgba(0, 0, 0, 0.627450980392157)";
        font-family: "华文隶书 Bold", "华文隶书";
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }
    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
  .login {
    width: 368px;
    margin: 0 auto;
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
  }
}
</style>
